﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using winproject.Models;

namespace winproject.Controllers
{
    public class ProductsController : Controller
    {
        //
        // GET: /Products/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Lists()
        {
            return View();
        }
        public ActionResult Details(ProductDetails oSearchItem = null)
        {
            if (Request.Form.HasKeys())
            {
                //check if data is avaible in post
                if (Request.Form.AllKeys.Contains("txtUniqueId"))
                {
                    oSearchItem = new ProductDetails();
                    oSearchItem.nPid = Convert.ToInt32(Request.Form["txtUniqueId"]);
                }
            }
            if (oSearchItem != null && oSearchItem.nPid > 0)
            {
                ProductsModel oProductDetail = new ProductsModel();
                List<ProductDetails> oPDetailList = null;
                ReturnResult oResult = oProductDetail.getProductDetails(out oPDetailList, oSearchItem);
                var x = oResult;


                ViewData["resCode"] = x.errCode;
                ViewData["resData"] = (oPDetailList != null && oPDetailList.Count > 0) ? oPDetailList[0] : null;
            }

            CategoryModel oCategory = new CategoryModel();
            List<CategoryDetails> lsCatList = null;
            ReturnResult oCatResResult = oCategory.getCategoryDetails(out lsCatList, null);
            if (oCatResResult != null && oCatResResult.errCode == (int)cEnums.eErrorCodes.Success)
            {
                ViewData["Categories"] = lsCatList;
            }
            else
            {
                ViewData["Categories"] = null;
            }

            
            return View();
        }
        public ActionResult ProductStatus(ProductStats oSearchItem = null)
        {
            if (Request.Form.HasKeys())
            {
                //check if data is avaible in post
                if (Request.Form.AllKeys.Contains("txtUniqueId"))
                {
                    oSearchItem = new ProductStats();
                    oSearchItem.nPid = Convert.ToInt32(Request.Form["txtUniqueId"]);
                }
            }
            if (oSearchItem != null && oSearchItem.nPid > 0)
            {
                ProductStatsModel oProductDetail = new ProductStatsModel();
                List<ProductStats> oPDetailList = null;
                ReturnResult oResult = oProductDetail.getProdStats(out oPDetailList, oSearchItem);
                var x = oResult;

                ViewData["pid"] = oSearchItem.nPid;
                ViewData["resCode"] = x.errCode;
                ViewData["resData"] = (oPDetailList != null && oPDetailList.Count > 0) ? oPDetailList[0] : null;

                ViewData["maxRanking"] = 5;
                return View();
            }
            else
            {
                //invalid request as PID is needed
                return RedirectToAction("Lists");
            }
        }

        #region Product Stats Add Section
        [HttpPost, ValidateInput(false)]
        public ActionResult AddProductStats()
        {
            ReturnResult oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.BadRequest, msgJson = "[]" };
            //string path = Server.MapPath("~/Content/VirusUploads");
            ProductStats oItemDetail = new ProductStats();
            if (Request.Form.HasKeys())
            {
                if (Request.Form.AllKeys.Contains("txtUniqueId"))
                {
                    oItemDetail.nPid = Convert.ToInt32(Request.Form["txtUniqueId"]);
                }
                if (Request.Form.AllKeys.Contains("drpRanking"))
                {
                    oItemDetail.nPRanking = Convert.ToInt32(Request.Form["drpRanking"]);
                }
                if (Request.Form.AllKeys.Contains("drpRating"))
                {
                    oItemDetail.nEditorsRating = Convert.ToInt32(Request.Form["drpRating"]);
                }

                if (Request.Form.AllKeys.Contains("drpDownloadsRating"))
                {
                    oItemDetail.nDownloadsRating = Convert.ToInt32(Request.Form["drpDownloadsRating"]);
                }
                if (Request.Form.AllKeys.Contains("txtDownloadsCount"))
                {
                    oItemDetail.nDownloadCount = Convert.ToInt32(Request.Form["txtDownloadsCount"]);
                }
                if (Request.Form.AllKeys.Contains("txtVisitorsCount"))
                {
                    oItemDetail.nVisitedCount = Convert.ToInt32(Request.Form["txtVisitorsCount"]);
                }
                

                if (oItemDetail != null && oItemDetail.nPid > 0)
                {
                    ProductStatsModel oItemModel = new ProductStatsModel();
                    if (oItemDetail.nPid > 0)
                    {
                        //edit request
                        oResult = oItemModel.AddNewProdStats(oItemDetail, true);
                    }
                    else
                        oResult = oItemModel.AddNewProdStats(oItemDetail);
                }
                else
                {
                    oResult.errCode = (int)cEnums.eErrorCodes.OperationFailed;
                    oResult.msgJson = "Please provide valid product stats";
                }
            }

            var globalModel = new GlobalViewModel()
            {
                ApplicationName = Convert.ToString(HttpContext.Application["SiteName"]),
                jsonServerString = JsonConvert.SerializeObject(oResult)

            };
            if (oResult.errCode == (int)cEnums.eErrorCodes.OperationFailed)
                return RedirectToAction("Details", oResult);
            else
                return RedirectToAction("Lists", oResult);
        }

        #endregion

        public ActionResult ProductProperties(ProductProperties oSearchItem = null)
        {
            if (Request.Form.HasKeys())
            {
                //check if data is avaible in post
                if (Request.Form.AllKeys.Contains("txtUniqueId"))
                {
                    oSearchItem = new ProductProperties();
                    oSearchItem.nPid = Convert.ToInt32(Request.Form["txtUniqueId"]);
                }
            }
            if (oSearchItem != null && oSearchItem.nPid > 0)
            {
                ProductPropertiesModel oProductDetail = new ProductPropertiesModel();
                List<ProductProperties> oPDetailList = null;
                ReturnResult oResult = oProductDetail.getProdProperties(out oPDetailList, oSearchItem);
                var x = oResult;

                ViewData["pid"] = oSearchItem.nPid;
                ViewData["resCode"] = x.errCode;
                ViewData["resData"] = (oPDetailList != null && oPDetailList.Count > 0) ? oPDetailList[0] : null;
                return View();
            }
            else
            {
                //invalid request as PID is needed
                return RedirectToAction("Lists");
            }
            
        }

        #region Product Property Add Section
        [HttpPost, ValidateInput(false)]
        public ActionResult AddProductProperty()
        {
            ReturnResult oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.BadRequest, msgJson = "[]" };
            //string path = Server.MapPath("~/Content/VirusUploads");
            ProductProperties oItemDetail = new ProductProperties();
            if (Request.Form.HasKeys())
            {
                if (Request.Form.AllKeys.Contains("txtUniqueId"))
                {
                    oItemDetail.nPid = Convert.ToInt32(Request.Form["txtUniqueId"]);
                }
                if (Request.Form.AllKeys.Contains("txtTocLink"))
                {
                    oItemDetail.sTocLink = Convert.ToString(Request.Form["txtTocLink"]);
                }
                if (Request.Form.AllKeys.Contains("txtEulaLink"))
                {
                    oItemDetail.sEulaLink = Convert.ToString(Request.Form["txtEulaLink"]);
                }
                if (Request.Form.AllKeys.Contains("txtExeName"))
                {
                    oItemDetail.sExeName = Convert.ToString(Request.Form["txtExeName"]);
                }
                if (Request.Form.AllKeys.Contains("txtMD5CheckSum"))
                {
                    oItemDetail.sMD5CheckSum = Convert.ToString(Request.Form["txtMD5CheckSum"]);
                }
                if (Request.Form.AllKeys.Contains("txtFilesSize"))
                {
                    oItemDetail.nSizeInBytes = Convert.ToInt32(Request.Form["txtFilesSize"]);
                }
                if (Request.Form.AllKeys.Contains("txtAuthorName"))
                {
                    oItemDetail.sAuthorName = Convert.ToString(Request.Form["txtAuthorName"]);
                }
                if (Request.Form.AllKeys.Contains("chkExeSigned"))
                {
                    oItemDetail._isDigitalSigned = (Convert.ToString(Request.Form["chkExeSigned"]) == "on" || Convert.ToString(Request.Form["chkExeSigned"]) == "1" || Convert.ToString(Request.Form["chkExeSigned"])=="true")?true:false;
                }
                if (Request.Form.AllKeys.Contains("chkMultilingual"))
                {
                    oItemDetail._isMultilingual = (Convert.ToString(Request.Form["chkMultilingual"]) == "on" || Convert.ToString(Request.Form["chkMultilingual"]) == "1" || Convert.ToString(Request.Form["chkMultilingual"]) == "true") ? true : false;
                }   

                if (oItemDetail != null && oItemDetail.nPid > 0)
                {
                    ProductPropertiesModel oItemModel = new ProductPropertiesModel();
                    if (oItemDetail.nPid > 0)
                    {
                        //edit request
                        oResult = oItemModel.AddNewProdProperty(oItemDetail, true);
                    }
                    else
                        oResult = oItemModel.AddNewProdProperty(oItemDetail);
                }
                else
                {
                    oResult.errCode = (int)cEnums.eErrorCodes.OperationFailed;
                    oResult.msgJson = "Please provide valid product property";
                }
            }

            var globalModel = new GlobalViewModel()
            {
                ApplicationName = Convert.ToString(HttpContext.Application["SiteName"]),
                jsonServerString = JsonConvert.SerializeObject(oResult)

            };
            if (oResult.errCode == (int)cEnums.eErrorCodes.OperationFailed)
                return RedirectToAction("Details", oResult);
            else
                return RedirectToAction("Lists", oResult);
        }

        #endregion

        //########################### Products ##############################

        public ActionResult Products(ProductDetails oSearchItem = null)
        {
            if (Request.Form.HasKeys())
            {
                //check if data is avaible in post
                if (Request.Form.AllKeys.Contains("txtUniqueId"))
                {
                    oSearchItem = new ProductDetails();
                    oSearchItem.nPid = Convert.ToInt32(Request.Form["txtUniqueId"]);
                }
            }
            if (oSearchItem != null && oSearchItem.nPid > 0)
            {
                ProductsModel oProductDetail = new ProductsModel();
                List<ProductDetails> oPDetailList = null;
                ReturnResult oResult = oProductDetail.getProductDetails(out oPDetailList, oSearchItem);
                var x = oResult;


                ViewData["resCode"] = x.errCode;
                ViewData["resData"] = (oPDetailList != null && oPDetailList.Count > 0) ? oPDetailList : null;
            }
            return View();
        }


        #region Product Details Add Section
        [HttpPost, ValidateInput(false)]
        public ActionResult AddProductDetails()
        {
            ReturnResult oResult = new ReturnResult() { errCode = (int)cEnums.eErrorCodes.BadRequest, msgJson = "[]" };
            //string path = Server.MapPath("~/Content/VirusUploads");
            ProductDetails oItemDetail = new ProductDetails();
            if (Request.Form.HasKeys())
            {
                if (Request.Form.AllKeys.Contains("txtUniqueId"))
                {
                    oItemDetail.nPid = Convert.ToInt32(Request.Form["txtUniqueId"]);
                }
                if (Request.Form.AllKeys.Contains("drpCategory"))
                {
                    oItemDetail.nCid = Convert.ToInt32(Request.Form["drpCategory"]);
                }
                if (Request.Form.AllKeys.Contains("drpSubCategory"))
                {
                    oItemDetail.nSCid = Convert.ToInt32(Request.Form["drpSubCategory"]);
                }

                if (Request.Form.AllKeys.Contains("txtProdName"))
                {
                    oItemDetail.sTitle = Convert.ToString(Request.Form["txtProdName"]);
                }
                if (Request.Form.AllKeys.Contains("txtProductSlug"))
                {
                    oItemDetail.sSlug = Convert.ToString(Request.Form["txtProductSlug"]);
                }
                if (Request.Form.AllKeys.Contains("txtProductDesc"))
                {
                    oItemDetail.sDesc = HttpUtility.HtmlEncode(Convert.ToString(Request.Form["txtProductDesc"]));
                }
                if (Request.Form.AllKeys.Contains("txtProdHomeLink"))
                {
                    oItemDetail.sHomelnk = Convert.ToString(Request.Form["txtProdHomeLink"]);
                }
                if (Request.Form.AllKeys.Contains("txtProdDownloadLink"))
                {
                    oItemDetail.sDownloadlnk = Convert.ToString(Request.Form["txtProdDownloadLink"]);
                }
                if (Request.Form.AllKeys.Contains("txtProdBuyNowLink"))
                {
                    oItemDetail.sBuyNowlnk = Convert.ToString(Request.Form["txtProdBuyNowLink"]);
                }
                if (Request.Form.AllKeys.Contains("txtProdCompany"))
                {
                    oItemDetail.sCompanyName = Convert.ToString(Request.Form["txtProdCompany"]);
                }
                if (Request.Form.AllKeys.Contains("drpCurrency"))
                {
                    oItemDetail.nCurrency = Convert.ToInt32(Request.Form["drpCurrency"]);
                }
                if (Request.Form.AllKeys.Contains("txtProdBasePrice"))
                {
                    oItemDetail.dBasePrice = Convert.ToDecimal(Request.Form["txtProdBasePrice"]);
                }
                if (Request.Form.AllKeys.Contains("txtProdDiscount"))
                {
                    oItemDetail.nDiscountOffered = Convert.ToInt32(Request.Form["txtProdDiscount"]);
                }
                if (Request.Form.AllKeys.Contains("txtProdNetPrice"))
                {
                    oItemDetail.dNetPrice = Convert.ToDecimal(Request.Form["txtProdNetPrice"]);
                }
                

                if (oItemDetail != null && oItemDetail.nCid>0 && oItemDetail.nSCid>0 && oItemDetail.nCurrency>0 && oItemDetail.dNetPrice>0 && oItemDetail.sTitle.Trim().Length > 0 && oItemDetail.sDesc.Trim().Length > 0)
                {
                    ProductsModel oItemModel = new ProductsModel();
                    if (oItemDetail.nPid > 0)
                    {
                        //edit request
                        oResult = oItemModel.AddNewProduct(oItemDetail, true);
                    }
                    else
                        oResult = oItemModel.AddNewProduct(oItemDetail);
                }
                else
                {
                    oResult.errCode = (int)cEnums.eErrorCodes.OperationFailed;
                    oResult.msgJson = "Please provide valid product details";
                }
            }

            var globalModel = new GlobalViewModel()
            {
                ApplicationName = Convert.ToString(HttpContext.Application["SiteName"]),
                jsonServerString = JsonConvert.SerializeObject(oResult)

            };
            if(oResult.errCode==(int)cEnums.eErrorCodes.OperationFailed)
                return RedirectToAction("Details", oResult);
            else
                return RedirectToAction("Lists", oResult);
        }

        #endregion

        #region Get Product Detail JSON String - Service (Ajax) Call
        public JsonResult GetProductDetailJson(ProductDetails oSearchItem)
        {
            ProductsModel oProductDetail = new ProductsModel();
            List<ProductDetails> oPDetailList = null;
            ReturnResult oResult = oProductDetail.getProductDetails(out oPDetailList, oSearchItem);
            return Json(oResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region Delete Product - Service (Ajax) Call
        public JsonResult DeleteProduct(ProductDetails oSearchItem)
        {
            ProductsModel oPDetail = new ProductsModel();
            ReturnResult oResult = oPDetail.deleteProduct(oSearchItem);
            return Json(oResult, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
